//
//  PopoverAnimator.swift
//  zhuWEIBO1
//
//  Created by STW-02 on 18/10/31.
//  Copyright © 2018年 STW-02. All rights reserved.
//

import UIKit

class PopoverAnimator: NSObject {
    //  标识视图是否弹出
    var  isPresented : Bool = false
    // 有外面控制 frame 的大小 
    var presentedFrame : CGRect = CGRect.zero
    // 保存titlbtn 的状态和 视图的显示和消失保持一致
    var callBack  : (_ presented :Bool) -> ()
    
     // 重写 init 方法 
    init(callBack :  @escaping (_ presented :Bool) -> ()) {
          self.callBack = callBack
    }
    
}
extension PopoverAnimator : UIViewControllerTransitioningDelegate{
    // 跳转的代理   目的 改变弹出view 的尺寸
     // 使用闭包时 在可以自动生成的地方 进行自动的生成 然后在粘贴到其他的地方
    
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
         let presentedController = XMGPresentationController(presentedViewController: presented, presenting: presenting)
          presentedController.presentedFrame = presentedFrame
        
        return  presentedController
    }
    
    // 自定消失的动画
    func  animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = false
        callBack(isPresented)

        return self
    }
    
    // 自定义弹出的动画
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresented  = true
        
        // 给回调的函数赋值 
         callBack(isPresented)
        
        return self
    }
}
// 弹出动画和消失动画代理的方法
extension PopoverAnimator  : UIViewControllerAnimatedTransitioning {
    // 动画执行的时间
    func  transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.5
    }
    // 获取转场的上下文  可以通过转场上下文获取弹出的view 和消失的view
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        
        
        isPresented ?  animationForPresentedView(transitionContext: transitionContext) : animationForDismissedView(transitionContext: transitionContext)
    }
    
    // 自定义弹出的动画 参数上下文对象
    fileprivate func animationForPresentedView(transitionContext : UIViewControllerContextTransitioning){
        // 获取弹出的view
        let PresentedView = transitionContext.view(forKey: UITransitionContextViewKey.to)!
        // 将弹出的view  添加到containerView 中
        transitionContext.containerView.addSubview(PresentedView)
        //执行动画
        PresentedView.transform =  CGAffineTransform(scaleX: 1.0, y: 0.0)
        
        PresentedView.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
        UIView.animate(withDuration: 1) {
            
        }
        UIView.animate(withDuration: 0.5, animations: {
            PresentedView.transform = CGAffineTransform.identity
        }) { (_) in
            // 必须告诉上下文已经完成动画
            transitionContext.completeTransition(true)
        }
    }
    // 自定义消失的动画
    fileprivate func  animationForDismissedView (transitionContext : UIViewControllerContextTransitioning){
        // 1、 获取消失的view
        let dismissView = transitionContext.view(forKey: UITransitionContextViewKey.from)
        
        // 2 执行动画
        
        UIView.animate(withDuration: 0.5, animations: {
            dismissView?.transform = CGAffineTransform(scaleX: 1
                , y: 0.00001)
        }) { (_) in
            dismissView?.removeFromSuperview() // 移除所有的动画
            //告诉上下文 你已经完成动画
            transitionContext.completeTransition(true)
            
        }
        
    }
}
